package com.zsr.example.config.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.dto.excel.SubjectData;
import com.zsr.example.entity.EduSubject;
import com.zsr.example.service.EduSubjectService;
import com.zsr.example.util.constants.ErrorEnum;

/**
 * 读取课程分类Excel监听器
 * @author zsr
 * @date 2021/6/10 21:55
 */
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //因为当前类不能叫spring进行管理，需要自己new，不能用@Autowired自动注入
    //需要操作数据库，需要参数构造器注入
    private EduSubjectService subjectService;//课程分类service

    public SubjectExcelListener() {
    }
    public SubjectExcelListener(EduSubjectService subjectService) {
        this.subjectService=subjectService;
    }

    //读取没行数据
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData==null)throw  new CommonJsonException(ErrorEnum.E_20001);
        //一行一行读取，每次读取有两个值，第一个值为一级分类，第二个为二级分离
        EduSubject existOneSubject = existOneSubject(subjectService, subjectData.getOneSubjectName());
        if(existOneSubject==null){//进行添加
            existOneSubject =new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());//一级分类名
            subjectService.save(existOneSubject);//添加一级分类
        }
        //获取一级对象的id值
        String pid=existOneSubject.getId();
        //System.out.println(pid);
        //二级对象
        EduSubject existTwoSubject = existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
        if(existTwoSubject==null){
            existTwoSubject=new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());//二级分类名称
            subjectService.save(existTwoSubject);//添加二级分类

        }
    }

    /**
     * 判断一级分类是否重复
     * @param subjectService subject服务
     * @param name 读取的表格单元值
     * @return
     */
    private EduSubject existOneSubject(EduSubjectService subjectService,String name){
        QueryWrapper<EduSubject> wrapper =new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject eduSubject=subjectService.getOne(wrapper);
        return eduSubject;
    }

    /**
     * 判断二级分类是否重复
     * @param subjectService subject服务
     * @param name 读取的表格单元值
     * @param pid 一级分类
     * @return
     */
    private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper =new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject eduSubject=subjectService.getOne(wrapper);
        return eduSubject;
    }

    //读取完后操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
